/*
 * CS_READ_MAIL_RECV_PRESENT.cpp
 *
 *  Created on: 2013. 5. 21.
 *      Author: the2
 */


#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"
#include "../../NLogic/MailBox.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/VirtualCurrency.h"
#include "../../GameLog.h"

namespace ProjectZ { namespace NCommand {

void SendFailReadMailRecvPresent(User * pUser, int res)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(res);
	*pAck << pUser->GetVirtualCurrency()->GetZen();
	*pAck << pUser->GetCharacterInfo().gold;
	pAck->MakePacket(CMD_SC_READ_MAIL_RECV_PRESENT);
	pUser->Send(pAck);
}

void Command::CS_READ_MAIL_RECV_PRESENT(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	uint64_t mailSeq	= 0;
	uint8_t giftType	= 0;

	*pPacket >> mailSeq >> giftType;

	NLogic::MailBox * pMailBox = pUser->GetMailBox();
	if (NULL == pMailBox)
	{
		DEBUGLOG("CS_READ_MAIL_RECV_PRESENT : pMailBox is NULL!!");
		return;
	}

	NLogic::MailBox::Mail * pMail = pMailBox->GetMailData(mailSeq);
	if (NULL == pMail)
	{
		DEBUGLOG("CS_READ_MAIL_RECV_PRESENT : pMail is NULL!!");
		return;
	}

	if (0 == (pMail->GetData().gift_status & giftType) )
	{
		DEBUGLOG("CS_READ_MAIL_RECV_PRESENT : 0 == (pMail->GetData().gift_status & giftType)");
		return;
	}


	if (giftType & EM_GIFT_GOLD)
	{
//		pUser->GetCharacterInfo().gold += pMail->GetData().gold;
		NLogic::NFunction::ModifyGold(pUser, pMail->GetData().gold);
		NGameLog::LogGoldInfo(pUser, MAIL, pMail->GetData().gold);
	}
	else if (giftType & EM_GIFT_ZEN)
	{
		if (0 <= pMail->GetData().gift_vcindex)
		{
			if (EM_VCINDEX_ADMIN_MAIL_EVENT == pMail->GetData().gift_vcindex)
			{
				NLogic::NFunction::VCProcess(pUser, EM_VCINDEX_ADMIN_MAIL_EVENT, pMail->GetData().zen);
			}
			else if (EM_VCINDEX_ADMIN_MAIL_CS == pMail->GetData().gift_vcindex)
			{
				NLogic::NFunction::VCProcess(pUser, EM_VCINDEX_ADMIN_MAIL_CS, pMail->GetData().zen);
			}
			else if (EM_VCINDEX_ADMIN_CROSSEVENT == pMail->GetData().gift_vcindex)
			{
				NLogic::NFunction::VCProcess(pUser, EM_VCINDEX_ADMIN_CROSSEVENT, pMail->GetData().zen);
			}
			else
			{
				NLogic::NFunction::VCProcess(pUser, pMail->GetData().gift_vcindex, pMail->GetData().zen);
			}
		}
	}
	else if (giftType & EM_GIFT_ITEM)
	{
		if (0 > pMail->GetData().item.tid || 0 > pMail->GetData().item.sub_type)
		{
			DEBUGLOG("CS_READ_MAIL_RECV_PRESENT : Ooops! INVALID ITEM. tid=%d, sheet=%d", pMail->GetData().item.tid, pMail->GetData().item.sub_type);
			return;
		}

		NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(pMail->GetData().item.bag_type));
		if (NULL == pBag)
		{
			DEBUGLOG("CS_READ_MAIL_RECV_PRESENT : pBag is NULL!!");
			return;
		}

		bool bBagFull = false;
		if (false == pBag->AddItemWithData(pMail->GetData().item, bBagFull, true))
		{
			if (true == bBagFull)
			{
				SendFailReadMailRecvPresent(pUser, ACK_INVEN_BAG_FULL);
				return;
			}

			DEBUGLOG("CS_READ_MAIL_RECV_PRESENT : false == pBag->AddItemWithData(clsData, true)");
			return;
		}
	}
	else
	{
		DEBUGLOG("CS_READ_MAIL_RECV_PRESENT : Unknown giftType!! clientGiftType=%d, mailGiftType=%d",
				giftType, pMail->GetData().gift_status);
		return;
	}

	pMail->GetData().gift_status = pMail->GetData().gift_status ^ giftType;
	pMail->UpdateGiftStatusDatabaseMail();

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << pUser->GetVirtualCurrency()->GetZen();
	*pAck << pUser->GetCharacterInfo().gold;
	pAck->MakePacket(CMD_SC_READ_MAIL_RECV_PRESENT);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
